
import csv

import os
import sys
import glob

import mysql
import tables

try:
  from generator.utils import config
except:
  from utils import config
  
class Model:
  
  def __init__(self):
    cfg = config.Config('project.cfg')
    self.dbschema = cfg.getOption('mysql','project_schema')  
    self.db = mysql.MySQL()

  def getTables(self):
    #cfg = config.Config('project.cfg')
    t = tables.Tables()
    return t.gettabs()
    

  def getTables00(self):
    sql = "show tables"
    sql = "select type, name from ttable"    
    all = self.db.fetchall(sql)
    tables = {}
    str = 'Tables_in_%s' %(self.dbschema)
    for x in all:
      tables[x['name']] = x['type']
      
      #tables.append(x[str])
    #print tables
    #sys.exit(0)
    return tables

  def getColumns00(self, table):
    
    sql = "select name as Field, data_type as Type from ccolumn where tablename ='%s'" %(table)
    all = self.db.fetchall(sql)
    return all

  def getTabsByShow(self):
    sql = "show tables"
    all = self.db.fetchall(sql)
    tables = []
    str = 'Tables_in_%s' %(self.dbschema)
    for x in all:
      tables.append(x[str])
    return tables



  def getColumns(self, table):
    
    sql = "show fields from %s" %(table)
    all = self.db.fetchall(sql)
    return all
    """
    for x in all:
      print x['Field'],
      print x['Type']
    """

if __name__ == "__main__":
  #tabs = gettables()
  #print tabs
  md =  Model()
  tabs = md.getColumns("oorder")
  print sys.modules
  """
  for item in sys.modules:
    print item
  """
  #print tabs
